package com.beney.ds.sorting;

import com.beney.ds.sorting.abs.AbstractHeap;

import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Queue;

public class HeapSort extends AbstractHeap {
    private HeapSort() {
    }

    // implement the heap myself
    public static void sort(Comparable[] a) {
        int last;
        toHeap(a, last = a.length - 1);
        for (int i = last; i >= 0; --i) {
            pop(a, i);
        }
    }

    // using api
    public static void sortX(Comparable[] a) {
        Queue<Comparable> pq = new PriorityQueue<>(Arrays.asList(a)); // default min-heap
        int len = a.length;
        for (int i = 0; i < len; ++i) {
            a[i] = pq.remove();
        }
    }
}
